package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.ProjectConstants;
import com.ruoyi.system.domain.SysExperts;
import com.ruoyi.system.domain.SysProjectInfo;
import com.ruoyi.system.service.ISysExpertsService;
import com.ruoyi.system.service.ISysHistoryService;
import com.ruoyi.system.service.ISysProjectInfoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysProjectExpert;
import com.ruoyi.system.service.ISysProjectExpertService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目评审Controller
 * 
 * @author riddou
 * @date 2022-04-15
 */
@RestController
@RequestMapping("/system/projectExpert")
public class SysProjectExpertController extends BaseController
{
    @Autowired
    private ISysProjectExpertService sysProjectExpertService;

    @Autowired
    private ISysProjectInfoService sysProjectInfoService;

    @Autowired
    private ISysHistoryService sysHistoryService;

    @Autowired
    private ISysExpertsService sysExpertsService;


    /**
     * 查询项目评审列表
     */
    @PreAuthorize("@ss.hasPermi('system:projectExpert:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysProjectExpert sysProjectExpert)
    {
        startPage();
        List<SysProjectExpert> list = sysProjectExpertService.selectSysProjectExpertList(sysProjectExpert);
        return getDataTable(list);
    }

    /**
     * 导出项目评审列表
     */
    @PreAuthorize("@ss.hasPermi('system:projectExpert:export')")
    @Log(title = "项目评审", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysProjectExpert sysProjectExpert)
    {
        List<SysProjectExpert> list = sysProjectExpertService.selectSysProjectExpertList(sysProjectExpert);
        ExcelUtil<SysProjectExpert> util = new ExcelUtil<SysProjectExpert>(SysProjectExpert.class);
        util.exportExcel(response, list, "项目评审数据");
    }

/*    *//**
     * 无用
     *//*
    @PreAuthorize("@ss.hasPermi('system:projectExpert:query')")
    @GetMapping(value = "/{expertId}")
    public AjaxResult getInfo(@PathVariable("expertId") Long expertId)
    {
        return AjaxResult.success(sysProjectExpertService.selectSysProjectExpertByExpertId(expertId));
    }

    *//**
     * 新增项目评审
     *//*
    @PreAuthorize("@ss.hasPermi('system:projectExpert:add')")
    @Log(title = "项目评审", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysProjectExpert sysProjectExpert)
    {
        return toAjax(sysProjectExpertService.insertSysProjectExpert(sysProjectExpert));
    }*/

    /**
     * 修改项目评审
     */
    @PreAuthorize("@ss.hasPermi('system:projectExpert:edit')")
    @Log(title = "项目评审", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysProjectExpert sysProjectExpert)
    {
        if (sysProjectExpert == null) return error("当前项目为空！");
        //获取项目信息
        SysProjectInfo sysProjectInfo = sysProjectInfoService.selectSysProjectInfoByProjectId(sysProjectExpert.getProjectId());

        SysExperts expert = sysExpertsService.selectSysExpertsByExpertId(sysProjectExpert.getExpertId());

        //审核通过
        if (ProjectConstants.RESULT_APPROVED.equals(sysProjectExpert.getResult())) {
            //更新审核状态
            sysProjectExpert.setProjectState(ProjectConstants.STATE_REVIEWED);
            sysProjectExpertService.updateSysProjectExpert(sysProjectExpert);
            //更新项目状态
            //当前是已提交状态，则为审批流程
            if (ProjectConstants.STATE_COMMITED.equals(sysProjectInfo.getState())) {
                sysProjectInfo.setState(ProjectConstants.STATE_APPROVED);
                // 实施跟踪流程 设置为该流程则会导致专家不能撤回
                //sysProjectInfo.setStage(ProjectConstants.STAGE_TRACKING);
                sysProjectInfoService.updateSysProjectInfo(sysProjectInfo);
                //专家审批数减少
                expert.setCurReviewCount(expert.getCurReviewCount() - 1);
                sysExpertsService.updateSysExperts(expert);
                //创建历史记录
                return toAjax(sysHistoryService.insertSysHistory(sysHistoryService.buildHistory(sysProjectExpert, ProjectConstants.TYPE_REVIEW, ProjectConstants.HIS_OPER_APPROVED)));
            }
            //当前是验收状态
            else {
                sysProjectInfo.setStage(ProjectConstants.STAGE_COMPLETE);
                sysProjectInfoService.updateSysProjectInfo(sysProjectInfo);
                //专家审批数减少
                expert.setCurReviewCount(expert.getCurReviewCount() - 1);
                sysExpertsService.updateSysExperts(expert);
                //创建历史记录
                return toAjax(sysHistoryService.insertSysHistory(sysHistoryService.buildHistory(sysProjectExpert, ProjectConstants.TYPE_ACCEPT, ProjectConstants.HIS_OPER_ACCEPTED)));
            }
        }
        //未通过
        else if (ProjectConstants.RESULT_DENIED.equals(sysProjectExpert.getResult())) {
            //更新审核状态
            sysProjectExpert.setProjectState(ProjectConstants.STATE_REVIEWED);
            sysProjectExpertService.updateSysProjectExpert(sysProjectExpert);
            //更新项目状态
            //当前是已提交状态，则为审批流程
            if (ProjectConstants.STATE_COMMITED.equals(sysProjectInfo.getState())) {
                sysProjectInfo.setState(ProjectConstants.STATE_UNCOMMITED);
                sysProjectInfo.setStage(ProjectConstants.STAGE_TO_EVALUATED); // 直接打回未提交
                sysProjectInfoService.updateSysProjectInfo(sysProjectInfo);
                //专家审批数减少
                expert.setCurReviewCount(expert.getCurReviewCount() - 1);
                sysExpertsService.updateSysExperts(expert);
                //创建历史记录
                return toAjax(sysHistoryService.insertSysHistory(sysHistoryService.buildHistory(sysProjectExpert, ProjectConstants.TYPE_REVIEW, ProjectConstants.HIS_OPER_REVIEW_DENIED)));
            }
            //当前是验收状态
            else {
                sysProjectInfo.setStage(ProjectConstants.STAGE_TRACKING);   //返回到实施跟踪
                sysProjectInfoService.updateSysProjectInfo(sysProjectInfo);
                //专家审批数减少
                expert.setCurReviewCount(expert.getCurReviewCount() - 1);
                sysExpertsService.updateSysExperts(expert);
                //创建历史记录
                return toAjax(sysHistoryService.insertSysHistory(sysHistoryService.buildHistory(sysProjectExpert, ProjectConstants.TYPE_ACCEPT, ProjectConstants.HIS_OPER_ACCEPT_DENIED)));
            }
        }
        //增加评论
        else {
            sysProjectInfo.setReviewResult(sysProjectExpert.getComment());
            sysProjectInfoService.updateSysProjectInfo(sysProjectInfo);
            return toAjax(sysProjectExpertService.updateSysProjectExpert(sysProjectExpert));
        }

    }

/*    *//**
     * 删除项目评审
     *//*
    @PreAuthorize("@ss.hasPermi('system:projectExpert:remove')")
    @Log(title = "项目评审", businessType = BusinessType.DELETE)
	@DeleteMapping("/{expertIds}")
    public AjaxResult remove(@PathVariable Long[] expertIds)
    {
        return toAjax(sysProjectExpertService.deleteSysProjectExpertByExpertIds(expertIds));
    }*/
}
